/**
 * Created by cyf on 15-12-1.
 * 好友树构建器
 */
(function() {
	qxchat.GroupTree = GroupTree;
	var EXTEND = null; //是否需要继承
	function GroupTree(treeViewId, zNodes) {
		if (EXTEND) {
			EXTEND.apply(this, arguments);
		}
		// 初始化菜单
		this.init();
		this.zTreeObj = null;
		this.groupTreeRMenu = $("#groupTreeRMenu");
		this.currentNode = null;
		var self = this;
		var setting = {
			edit: {
				enable: true,
				showRenameBtn: false,
				showRemoveBtn: false
			},
			drag: {
				prev: true,
				next: true,
				inner: false
			},
			view: {
				showLine: false,
				showIcon: false,
				dblClickExpand: true,
				addDiyDom: addDiyDom
			},
			data: {
				simpleData: {
					enable: true
				}
			},
			callback: {
				onClick: onClick,
				beforeRename: beforeRename,
				onRightClick: onRightClick
			}
		};
		//点击事件
		function onClick(event, treeId, treeNode, clickFlag) {
			//是否可点击，可理解为是否为用户节点，如果是打开窗口
			if (!(treeNode.click == false)) {
				global.groupChatWinHandler.open(treeNode.id, treeNode.name)
			} else {
				//self.zTreeObj.expandNode(treeNode)
			}
		}
		//右键事件
		function onRightClick(event, treeId, treeNode) {
			self.currentNode = treeNode;
			if (!treeNode && event.target.tagName.toLowerCase() != "button" && $(event.target).parents("a").length == 0) {
				self.zTreeObj.cancelSelectedNode();
				showGroupTreeRMenu("root", event.clientX, event.clientY);
			} else if (treeNode && !treeNode.noR) {
				self.zTreeObj.selectNode(treeNode);
				showGroupTreeRMenu("node", event.clientX, event.clientY);
			}
		}
		//显示右键菜单
		function showGroupTreeRMenu(type, x, y) {
			if (type == "root") {
				$("#groupTree_edit").hide();
			} else {
				$("#groupTree_edit").show();
			}
			$("#groupTreeRMenu ul").show();
			var width = self.groupTreeRMenu.width();
			var height = self.groupTreeRMenu.height();
			if ((x + width) > document.body.offsetWidth) {
				x -= (x + width) - document.body.offsetWidth + 10;
			}
			if ((y + height) > document.body.offsetHeight) {
				y -= (y + height) - document.body.offsetHeight + 10;
			}
			self.groupTreeRMenu.css({
				"top": y + "px",
				"left": x + "px",
				"visibility": "visible"
			});
			$("body").bind("mousedown", onBodyMouseDown);
		}
		//点击其它位置隐藏菜单
		function onBodyMouseDown(event) {
			if (!(event.target.id == "groupTreeRMenu" || $(event.target).parents("#groupTreeRMenu").length > 0)) {
				self.groupTreeRMenu.css({
					"visibility": "hidden"
				});
			}
		}
		//添加分组
		function addTreeNode() {
			hideGroupTreeRMenu();
			global.groupInfoWinHandler.open();
		}
		$("#groupTree_add").bind("click", addTreeNode);
		//编辑分组
		function editTreeNode() {
			hideGroupTreeRMenu();
			//self.zTreeObj.editName(self.currentNode);
			global.groupInfoWinHandler.open(self.currentNode);
		}
		$("#groupTree_edit").bind("click", editTreeNode);

		function beforeRename(treeId, treeNode, newName) {
			if (newName.length == 0) {
				alert("节点名称不能为空.");
				setTimeout(function() {
					self.zTreeObj.editName(treeNode)
				}, 10);
				return false;
			}
			var message = '{"data":{"loginName":"' + global.currentUserLoginName + '","groupId":"' + treeNode.id + '","groupName":"' + newName + '","type":"GroupEdit",},"type":"event"}';
			global.webSocketHandler.send(message);
			return true;
		}

		//隐藏菜单
		function hideGroupTreeRMenu() {
			if (self.groupTreeRMenu) self.groupTreeRMenu.css({
				"visibility": "hidden"
			});
			$("body").unbind("mousedown", onBodyMouseDown);
		}
		//初始化联系人样式
		function addDiyDom(treeId, treeNode) {
			var spaceWidth = 5;
			var switchObj = $("#" + treeNode.tId + "_switch")
			var icoObj = $("#" + treeNode.tId + "_ico");
			var text = $("#" + treeNode.tId + "_span");
			var spaceStr = "<span id='" + treeNode.tId + "_span_status' style='padding-left:15px'></span>";
			text.append(spaceStr);
			var ustatus = $("#" + treeNode.tId + "_span_status");
			switchObj.remove();
			icoObj.before(switchObj);

			if (treeNode.level > 1) {
				var spaceStr = "<span style='display: inline-block;width:" + (spaceWidth * treeNode.level) + "px'></span>";
				switchObj.before(spaceStr);
			}
			switch (treeNode.status) {
				case 0:
					ustatus.css("color", "gray");
					ustatus.html("离线");
					break;
				case 1:
					ustatus.css("color", "green");
					ustatus.html("在线");
					break;
			}
		}
		var treeObj = $("#" + treeViewId);
		//构建树
		$.fn.zTree.init(treeObj, setting, zNodes);
		treeObj.addClass("showIcon");
		//当前zTree对象
		this.zTreeObj = $.fn.zTree.getZTreeObj(treeViewId);
		this.zTreeObj.expandAll(true);
		this.zTreeObj.expandAll(false);
	}
	GroupTree.prototype = {
		init: function() {
			$(document.body).append('<div id="groupTreeRMenu" class="rMenu">' + '<ul>' + '	<li id="groupTree_add">新建群组</li>' + '	<li id="groupTree_edit">管理群组</li>' + '</ul>' + '</div>');
		}
	}
})();